// contentPackage/pages/videoDetail/videoDetail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLike: false,
    isCollection: false,
    text: '', //评论内容

    showPro: true,
    videoInfo: '',
    product: '',
    comments: [], //评论列表
    showCom: true
  },

  // 商品详情
  toDetail() {
    var id = this.data.videoInfo.productId;
    wx.navigateTo({
      url: '/mainPackage/pages/productDetail/productDetail?id=' + id,
    })
  },

  // 发表评论
  sendHandle() {
    var text = this.data.text;
    if (text == '') {
      wx.showToast({
        title: '请输入评论内容',
        icon: 'none'
      })
      return
    }
    console.log(text);

  },

  // 关闭弹窗
  onClose() {

    this.setData({
      showCom: false
    })

  },

  // 输入框编辑
  inputHandle(e) {
    var str = e.currentTarget.dataset.flag
    var val = e.detail.value
    this.setData({
      [str]: val
    })

    console.log(this.data);
  },


  //   点赞  收藏

  conHandle(e) {
    console.log(e);
    var flag = e.currentTarget.dataset.flag;


    if (flag == 'like') {

      this.setData({
        isLike: !this.data.isLike
      })
      wx.showToast({
        title: this.data.isLike ? '已点赞' : '取消点赞',
        icon: 'none'
      })

    } else if (flag == 'collection') {
      this.setData({
        isCollection: !this.data.isCollection
      })
      wx.showToast({
        title: this.data.isCollection ? '已收藏' : '取消收藏',
        icon: 'none'
      })


    }
  },

  // 关闭商品弹窗
  closeHandle() {
    this.setData({
      showPro: false
    })
  },

  getVideoDetail() {
    var url = app.globalData.url + "ScBaiKe/listBaiKeComment",
      isLike = false,
      isCollection = false,
      comments = [],
      that = this;
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        baiKeId: that.data.videoInfo.baikeId,
        productId: that.data.videoInfo.productId || 0,
        userId: app.globalData.userId,
      }, res => {
        console.log(res);
        if (res.flag) {
          var res = res.data
          wx.hideLoading()
          isLike = res.collect.includes('0')
          isCollection = res.collect.includes('1')

          comments = res.comments
          that.setData({
            product: res.product,
            isLike,
            isCollection,
            comments
          })


          console.log(res);
        }
      },
      err => {
        console.log(err);
      })

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);

    this.setData({
      videoInfo: JSON.parse(options.obj),
      showPro: JSON.parse(options.obj).productId ? true : false
    })
    console.log(this.data.videoInfo);
    this.getVideoDetail()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})